package com.situ.mall.web.controller;

import com.situ.mall.common.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

//注册页面
@Controller
@RequestMapping("/")
public class RegisterController {
    @Autowired
    private IMemberService memberService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    //返回注册界面
    @GetMapping("/register")
    public String register(HttpSession session, Model model) {
        Object attribute = session.getAttribute("currentUser");
        if (attribute != null) {
            model.addAttribute("one", new Object());
            session.removeAttribute("currentUser");
        }
        return "register";
    }

    @PostMapping("/register/add")
    @ResponseBody
    public String add(String tel,String pas,String code,String uname){
        //先判断code对不对
        Object o =  redisTemplate.opsForValue().get("reg-" + tel);
        if (o == null || (String.valueOf(o)).equals(code)){
            /*throw new BusinessException("验证码不正确");*/
        }
        //派memberService去注册这个账号
        memberService.saveMember(tel,pas,uname);
        return "redirect:/login";
    }
}
